<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>客户端</title>
  </head>
  <body>
    <button class="btn">发送消息</button>
    <script>
      // 连接上WebSocket服务器
      const socket = new WebSocket("ws://localhost:8080");

      // 接受服务器发送的消息
      socket.onmessage = function (e) {
        console.log(e.data);
      };

      document.querySelector(".btn").onclick = function () {
        socket.send("客户端给服务器发送消息~~~");
      };
    </script>
  </body>
</html>
